<?php
class MembersController extends Zend_Controller_Action 
{
	private $jobseeker;
	public function init()
	{
		$this->jobseeker = new Zend_Session_Namespace('jobseeker');
		$seconds = 86400; //24 hour
		$this->jobseeker->setExpirationSeconds($seconds);
		
		$this->view->controller = $this->_request->getControllerName();
		$this->view->action = $this->_request->getActionName();	
		if($this->jobseeker->member_id > 0)
		{							
		//	$this->_redirect(DOMAIN);
		}
		
		$this->objMembers = new Application_Model_Members();
	}

	public function indexAction()
	{
		$arrParam = array();
		$arrError = array();
		if($this->_request->isPost()){
			$arrParam['member_email'] 				= $this->_request->getParam('member_email','');
			$arrParam['member_password'] 			= $this->_request->getParam('member_password','');
			$arrParam['member_re_password'] 		= $this->_request->getParam('member_re_password','');
			$arrParam['member_firstname'] 			= $this->_request->getParam('member_firstname','');
			$arrParam['member_lastname'] 			= $this->_request->getParam('member_lastname','');
			$arrParam['location_id'] 				= $this->_request->getParam('location_id',0);
			$arrParam['indistry_id'] 				= $this->_request->getParam('indistry_id',0);
			$arrParam['member_address'] 			= $this->_request->getParam('member_address','');
			$arrParam['member_birthday'] 			= $this->_request->getParam('member_birthday','');
			$arrParam['member_phone'] 				= $this->_request->getParam('member_phone','');
			$arrParam['level_id'] 					= $this->_request->getParam('level_id',0);
			$arrParam['degree_id'] 					= $this->_request->getParam('degree_id',0);
			$arrParam['member_merritial'] 			= $this->_request->getParam('member_merritial',0);
			$arrParam['member_gender'] 				= $this->_request->getParam('member_gender',0);
			$arrParam['member_language_level'] 		= $this->_request->getParam('member_language_level','');
			$arrParam['member_picture'] 			= $this->_request->getParam('member_picture','');
			$valiEmail = new Zend_Validate_EmailAddress();
			if($arrParam['member_email'] == "")
				$arrError['member_email'] = 1;
			else if(!$valiEmail->isValid($arrParam['member_email']))
				$arrError['member_email'] = 2;
			else if($this->objMembers->checkMemberEmail($arrParam['member_email']) == 1)
				$arrError['member_email'] = 3;
			if($arrParam['member_password'] == "")
				$arrError['member_password'] = 1;
			else if($arrParam['member_password'] != $arrParam['member_re_password'])
				$arrError['member_password'] = 2;
			
			if(empty($arrError)){
				$intMemberId = $this->objMembers->insertMember($arrParam);
				if($intMemberId > 0) {
					//send email active;
					$time = time();
					$code_active = $time."::".$arrParam['member_email']."::".$arrParam['member_password']."::".KEYPASS."::".md5($time.$arrParam['member_email'].$arrParam['member_password'].KEYPASS);
					$encode_active = base64_encode($code_active);
					$link_active = DOMAIN.'/jobseekers/active?code='.$encode_active;
					//redirect to page register success
				} else {
					$arrError['member_insert_fail'] = 1;
				}
			}
		}
		$this->view->arrParam = $arrParam;
		$this->view->arrError = $arrError;
		var_dump($arrParam);
		var_dump($arrError);
		exit;
	}
	
	public function updateAction()
	{
		if($this->jobseeker->member_id <= 0) {							
			$this->_redirect(DOMAIN.'/login');
		}
		$arrParam = array();
		$arrError = array();
		if($this->_request->isPost()){
			
			$arrParam['member_password'] 			= $this->_request->getParam('member_password','');
			$arrParam['member_password_old']		= $this->_request->getParam('member_password_old','');
			$arrParam['member_firstname'] 			= $this->_request->getParam('member_firstname','');
			$arrParam['member_lastname'] 			= $this->_request->getParam('member_lastname','');
			$arrParam['location_id'] 				= $this->_request->getParam('location_id',0);
			$arrParam['indistry_id'] 				= $this->_request->getParam('indistry_id',0);
			$arrParam['member_address'] 			= $this->_request->getParam('member_address','');
			$arrParam['member_birthday'] 			= $this->_request->getParam('member_birthday','');
			$arrParam['member_phone'] 				= $this->_request->getParam('member_phone','');
			$arrParam['level_id'] 					= $this->_request->getParam('level_id',0);
			$arrParam['degree_id'] 					= $this->_request->getParam('degree_id',0);
			$arrParam['member_merritial'] 			= $this->_request->getParam('member_merritial',0);
			$arrParam['member_gender'] 				= $this->_request->getParam('member_gender',0);
			$arrParam['member_language_level'] 		= $this->_request->getParam('member_language_level','');
			$arrParam['member_picture'] 			= $this->_request->getParam('member_picture','');
//			if($arrParam['member_password'] == "")
//				$arrError['member_password'] = 1;
			
			if(empty($arrError)){
				$intMemberId = $this->objMembers->updateMember($arrParam);
				if($intMemberId > 0) {
					
					//redirect to page register success
				} else {
					$arrError['member_insert_fail'] = 1;
				}
			}
		}
		$this->view->arrParam = $arrParam;
		$this->view->arrError = $arrError;
		var_dump($arrParam);
		var_dump($arrError);
		
		
		$arrData = $this->objMembers->getDetailMember($this->jobseeker->member_email);
		$this->view->arrData = $arrData;
		exit;
	}
	
	public function infoAction()
	{
		if($this->jobseeker->member_id <= 0) {							
			$this->_redirect(DOMAIN.'/login');
		}
		$arrData = $this->objMembers->getDetailMember($this->jobseeker->member_email);
		$this->view->arrData = $arrData;
		exit;
	}

	public function loginAction()
	{
		//echo phpinfo(); exit;
		$_POST['member_email'] = 'quocviet1501@gmail.com';
		$_POST['member_password'] = '1234567';
		//if($this->_request->isPost())
		{
			$filters = array(	'member_email'  	=> array('StripTags','StringTrim'),
								'member_password' 	=> array('StripTags','StringTrim') );	
			$validate = array(	'member_email' 		=> array(	new Zend_Validate_NotEmpty(),
																new Zend_Validate_EmailAddress()),
								'member_password'	=> array(	new Zend_Validate_NotEmpty(), 
																new Zend_Validate_StringLength(array('min' => 6,'max' => 30))));
			$input = new Zend_Filter_Input($filters,$validate,$_POST);
			if ($input->isValid()) {
				$arrParam = array();
				$arrParam['member_email'] = $input->member_email;
				$arrParam['member_password'] = md5($input->member_password);
				$authResult = array();
				$arrResults = $this->objMembers->loginMember($arrParam);
				if($arrResults != NULL)	{
					$this->jobseeker->member_id 		= $arrResults[0]['MEMBER_ID'];
					$this->jobseeker->member_email 		= $arrResults[0]['MEMBER_EMAIL'];
				} else {
					$arrError['fail'] = 2;
				}
				
			} else {
				$arrError['fail'] = 1;
			}
		}
		//var_dump($this->jobseeker->member_id);
		var_dump($arrResults);
		var_dump($arrParam); 
		var_dump($arrError);
		exit;
	}
	public function logoutAction()
	{
		Zend_Session::namespaceUnset('jobseeker');
		exit;
	}
}
?>